/* =================================================
 * Package:			dk.iha.toksEditor
 * File:			SMSService.java
 * Description:		This is the service which 
 * 					monitors incomming SMS's.
 * 					A notification is sent if they 
 * 					contain a CPR.
 * 
 * Responsible
 * =================================================
 * Name:			Bjarke Jensen
 * Study number:	09676
 * 
 * Assistant(s)
 * =================================================
 * Name:			Jakob Sulby
 * Study number:	09136
 */

package dk.iha.toksEditor;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.IBinder;
import android.telephony.SmsMessage;

public class SMSService extends Service {
	/**
	 * @see android.app.Service#onBind(Intent)
	 */
	
	private String SMSReceived = "android.provider.Telephony.SMS_RECEIVED";
	private int mSMSService = 6;
	
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		IntentFilter filter = new IntentFilter(SMSReceived);
		registerReceiver(receiver, filter);
		return Service.START_REDELIVER_INTENT;
	}
	
	private final BroadcastReceiver receiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {

			if (intent.getAction().equals(SMSReceived)) {
				Bundle bundle = intent.getExtras();
				if (bundle != null) 
				{
					Object[] pdus = (Object[]) bundle.get("pdus");
					SmsMessage[] messages = new SmsMessage[pdus.length];
					for (int i = 0; i < pdus.length; i++)
						messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
					handleMessage(messages);
				}
			}
		}
	};
	
	private void handleMessage(SmsMessage[] messages)
	{
		for(int i = 0; i < messages.length;i++)
		{
			if(isCPR(messages[i].getMessageBody()))
			{
				NotificationManager notify = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		    	Notification notification = new Notification();
		    	
		    	notification.icon = R.drawable.toks_notification_icon;
		    	notification.tickerText = getString(R.string.notificationTickerText);
		    	notification.when = System.currentTimeMillis();
		    	
		    	notification.flags = Notification.FLAG_AUTO_CANCEL;
		    	
		    	Intent notifyIntent = new Intent(this, MainView.class);
				notifyIntent.setAction("android.intent.action.MAIN");
				notifyIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
				notifyIntent.addCategory("android.intent.category.LAUNCHER");
				String CPR = messages[i].getMessageBody();
				notifyIntent.putExtra("message", CPR);
				PendingIntent contentIntent = PendingIntent.getActivity(this, mSMSService++, notifyIntent, 0);
				
				CPR = getEasyCPR(CPR);
		    	
		    	notification.setLatestEventInfo(getApplicationContext(), getString(R.string.notificationTickerText), getString(R.string.notificationDiscText)+" "+CPR, contentIntent);
		    	
		    	notify.notify(1, notification);
			}
		}
	}
	
	private Boolean isCPR(String CPR)
	{
		if(CPR.length() == 10 && isNumeric(CPR))
			return true;
		return false;
	}
	private boolean isNumeric(String str)
	{
		return str.matches("-?\\d+(.\\d+)?");
	}
	
	private String getEasyCPR(String CPR){
		 StringBuilder patientEasyCPR = new StringBuilder();
		 patientEasyCPR.append(CPR.substring(0, 6));
		 patientEasyCPR.append('-');
		 patientEasyCPR.append(CPR.substring(6, 10));
		 return patientEasyCPR.toString();
	}

		
}
